package com.hy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hy.AppHttpCodeEnum;
import com.hy.Result.ResponseResult;
import com.hy.common.PageResponseResult;
import com.hy.file.service.FileStorageService;
import com.hy.mapper.WmMaterialMapper;
import com.hy.pojo.WmMaterial;
import com.hy.pojo.WmMaterialDto;
import com.hy.service.WmMaterialService;
import com.hy.util.WmThreadLocalUtil;
import io.minio.errors.*;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.UUID;

@Slf4j
@Service
@Transactional
public class WmMaterialServiceImpl  extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {


    @Autowired
    private FileStorageService fileStorageService;

    @Autowired
    private WmMaterialMapper wmMaterialMapper;
    /**
     * 图片上传
     * @param multipartFile
     * @return
     */
    @SneakyThrows
    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) {


        //1.检查参数
        if (multipartFile == null || multipartFile.getSize() <= 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.上传图片到minio中
        String path = fileStorageService.uploadImgFile("material", multipartFile.getOriginalFilename(),
                multipartFile.getInputStream());
        //3.把数据保存到数据库
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUserId(WmThreadLocalUtil.getUser().getId());
        wmMaterial.setUrl(path);
        wmMaterial.setType((short) 0);
        wmMaterial.setIsCollection((short) 0);
        wmMaterial.setCreatedTime(new Date());
        wmMaterialMapper.insert(wmMaterial);
        //4.返回结果
        return ResponseResult.okResult(wmMaterial);
    }

    @Override
    public ResponseResult findList(WmMaterialDto dto) {
        //1.检查参数
        dto.checkParam();
        //2.分页查询
        IPage page = new Page(dto.getPage(), dto.getSize());
        QueryWrapper<WmMaterial> wmMaterialQueryWrapper = new QueryWrapper<>();
        //根据用户id查询
        wmMaterialQueryWrapper.lambda().eq(WmMaterial::getUserId, WmThreadLocalUtil.getUser().getId());
        //按照时间倒叙
        wmMaterialQueryWrapper.orderByDesc("created_time");
        //是否收藏
        wmMaterialQueryWrapper.lambda().eq(dto.getIsCollection() != null, WmMaterial::getIsCollection, dto.getIsCollection());

        page = wmMaterialMapper.selectPage(page, wmMaterialQueryWrapper);

        //3.返回结果
//        ResponseResult result=new PageResponseResult(dto.getPage(),dto.getSize(),(int)page.getTotal());
//        result.setData(page.getRecords());
        return ResponseResult.okResult(200, "成功",page.getRecords());
    }
}
